{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "from imutils.video import VideoStream\n",
    "import os\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "localPath_labels = \"coco.names\"\n",
    "localPath_weights = \"yolov3.weights\"\n",
    "localPath_config = \"yolov3.cfg\"\n",
    "labels = open(localPath_labels).read().strip().split(\"\\n\")\n",
    "scaling = 0.005\n",
    "confidence_threshold = 0.5\n",
    "nms_threshold = 0.005  # Non Maxima Supression Threshold Vlue\n",
    "model = cv2.dnn.readNetFromDarknet(localPath_config, localPath_weights)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<built-in method getLayerNames of cv2.dnn_Net object at 0x10dc5de90>\n"
     ]
    }
   ],
   "source": [
    "#Start the video streat\n",
    "cap = VideoStream(src=0).start()\n",
    "#Getting the layers here\n",
    "layers_name = model.getLayerNames()\n",
    "\n",
    "output_layer = [layers_name[i[0] - 1] for i in model.getUnconnectedOutLayers()]\n",
    "print(model.getLayerNames)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "while True:\n",
    "    frame = cap.read()\n",
    "    (h, w) = frame.shape[:2]\n",
    "    blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False)\n",
    "    model.setInput(blob)\n",
    "    nnoutputs = model.forward(output_layer)\n",
    "    confidence_scores = []\n",
    "    box_dimensions = []\n",
    "    class_ids = []\n",
    "\n",
    "    for output in nnoutputs:\n",
    "        for detection in output:\n",
    "            scores = detection[5:]\n",
    "            class_id = np.argmax(scores)\n",
    "            confidence = scores[class_id]\n",
    "            if confidence > confidence_threshold :\n",
    "                box = detection[0:4] * np.array([w, h, w, h])\n",
    "                (center_x, center_y, width, height) = box.astype(\"int\")\n",
    "                x = int(center_x - (width / 2))\n",
    "                y = int(center_y - (height / 2))\n",
    "                box_dimensions.append([x, y, int(width), int(height)])\n",
    "                confidence_scores.append(float(confidence))\n",
    "                class_ids.append(class_id)\n",
    "    ind = cv2.dnn.NMSBoxes(box_dimensions, confidence_scores, confidence_threshold, nms_threshold)\n",
    "    for i in ind:\n",
    "        i = i[0]\n",
    "        (x, y, w, h) = (box_dimensions[i][0], box_dimensions[i][1],box_dimensions[i][2], box_dimensions[i][3])\n",
    "        cv2.rectangle(frame,(x, y), (x + w, y + h), (0, 255, 255), 2)\n",
    "        label = \"{}: {:.4f}\".format(labels[class_ids[i]], confidence_scores[i])\n",
    "        cv2.putText(frame, label, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,255), 2)\n",
    "    cv2.imshow(\"Yolo\", frame)\n",
    "    if cv2.waitKey(1) & 0xFF == ord(\"q\"):\n",
    "        break\n",
    "cv2.destroyAllWindows()\n",
    "cap.stop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
